package com.karst.essentials.chapter09
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.core.animation.clip.AnimationClip3D;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.special.CompositeMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.layer.ViewportLayer;
	
	internal class ViewportLayersExample extends BasicView
	{
		private var rotX : Number = .1 ;
		private var rotY : Number = .1 ;
		private var easeOut : Number = .1 ;
		private var camYaw : Number = 270 ;
		private var camPitch : Number = 90 ;
		
		private var mill : DAE = null ;
		private var floor : Plane = null ;
		
		
		public function ViewportLayersExample()
		{
			super( 640, 480, true, true );
			createView() ;
			startRendering() ;
		}
		
		private function createView() : void
		{
			if( mill == null )
			{
				mill = new DAE( true, null, true ) ;
				mill.addEventListener( FileLoadEvent.LOAD_COMPLETE, loadedModelHandler ) ;
				DAE( mill ).load( "animatedMill.dae" ) ;
				DAE(mill).addFileSearchPath( "." ) ; // 材质搜索文件夹
			}
			
			if( floor == null )
			{
				var colorMat : ColorMaterial = new ColorMaterial( 0x006600 ) ;
				var wireMat : WireframeMaterial = new WireframeMaterial() ;
				var floorMat : CompositeMaterial = new CompositeMaterial() ;
				floorMat.addMaterial( colorMat ) ;
				floorMat.addMaterial( wireMat ) ;
				floorMat.doubleSided = true ;
				floor = new Plane( floorMat, 1000, 1000, 1, 1 ) ;
				floor.y = -410 ;
				floor.rotationX = 90 ;
			}
			scene.addChild( floor ) ;
		}
		
		private function loadedModelHandler( $event : FileLoadEvent ) : void
		{
			var animationRight : AnimationClip3D = new AnimationClip3D( "right", 0, 6 ) ;
			var animationLeft : AnimationClip3D = new AnimationClip3D( "left", 6, 12 ) ;
			DAE( mill ).animation.addClip( animationRight ) ;
			DAE( mill ).animation.addClip( animationLeft ) ;
			DAE( mill ).play( "right" ) ;
			scene.addChild( mill ) ;
			addEventListener( MouseEvent.CLICK, clickHandler ) ;
			
			/** Example 1 
			 * 用 useOwnContainer 属性创建一个viewport 层
			 mill.useOwnContainer = true;
			 */
			
			/** Example 2
			 * 用 getChildLayer 方法创建viewport 层并给viewport 层排序
			var millLayer : ViewportLayer = viewport.getChildLayer(mill,true);
			*/
			
			/** Example 3 
			 * viewport层 用法演示
			var millLayer:ViewportLayer = viewport.getChildLayer(mill, true);
			millLayer.addDisplayObject3D(floor);
			*/
			
			/** Example 4
			 * 实例化ViewportLayer来创建 viewport 层
			 */
			var millLayer:ViewportLayer = new ViewportLayer(viewport,null);
			viewport.containerSprite.addLayer(millLayer);
			millLayer.addDisplayObject3D(mill,true);
		}
		
		private function clickHandler( $event : MouseEvent ) : void
		{
			if( mill )
			{
				DAE( mill ).stop() ;
				DAE( mill ).play( Math.random()<.5 ? "left" : "right" ) ;
			}
		}
		
		override protected function onRenderTick( $event : Event = null ) : void
		{
			super.onRenderTick( $event ) ;
			camPitch += (( mouseY - AsWingManager.getStage().stageHeight*.5) * rotX - camPitch + 90 ) * easeOut ;
			camYaw += (( mouseX - AsWingManager.getStage().stageWidth*.5) * rotY - camYaw + 270 ) * easeOut ;
			camera.orbit( camPitch, camYaw ) ;
		}
	}
}